package com.construction.api.controller.ocr;

import com.construction.api.controller.BaseController;
import com.construction.api.controller.DataResponse;
import com.construction.api.controller.ocr.vo.IdCardBackRsp;
import com.construction.api.controller.ocr.vo.IdCardFaceRsp;
import com.construction.api.controller.ocr.vo.IdCardRsp;
import com.construction.api.util.ocr.BaiDuConstant;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 1.身份证识别上传正面，反面，返回识别结果（让前端对比校验正确性）
 * 2.人员信息添加接口（包括人员信息，人员照片file，身份证file）
 */
@Controller
@RequestMapping(value = "/api")
public class IdCardController extends BaseController {

    @PostMapping("/idCard/uploadFile")
    @ResponseBody
    public Object uploadPost(@RequestParam("face") MultipartFile face,
                             @RequestParam("back") MultipartFile back) throws IOException {

//        PayloadHolder.projectId();//获取projectId

        if (face.isEmpty() || back.isEmpty()) {
            return DataResponse.error("正面或者反面未上传");
        }
        IdCardFaceRsp faceRsp = idCardFace(face.getBytes());
        IdCardBackRsp backRsp = idCardBack(back.getBytes());
        return new IdCardRsp(faceRsp, backRsp);

    }

    public IdCardBackRsp idCardBack(byte[] backImg) {
        JSONObject backRsp = BaiDuConstant.aipOcr.idcard(backImg, "back", new HashMap<>());
        IdCardBackRsp response = new IdCardBackRsp();

        if (backRsp.has("image_status")) {
            response.setImageStatus(backRsp.getString("image_status"));
        }

        if (response.success()) {
            backRsp.getJSONObject("words_result").toMap().forEach(new BiConsumer<String, Object>() {
                @Override
                public void accept(String s, Object o) {
                    String value = (String) ((Map) o).get("words");
                    switch (s) {
                        case "失效日期": {
                            response.setEndDate(value);
                            break;
                        }
                        case "签发机关": {
                            response.setIssue(value);
                            break;

                        }
                        case "签发日期": {
                            response.setStartDate(value);
                            break;
                        }
                    }
                }
            });
        }
        return response;
    }


    public IdCardFaceRsp idCardFace(byte[] faceImg) {
        JSONObject faceRsp = BaiDuConstant.aipOcr.idcard(faceImg, "front", new HashMap<>());

        IdCardFaceRsp response = new IdCardFaceRsp();

        if (faceRsp.has("idcard_number_type")) {
            response.setIdcardNumberType(faceRsp.getInt("idcard_number_type"));
        }

        if (faceRsp.has("image_status")) {
            response.setImageStatus(faceRsp.getString("image_status"));
        }


        if (response.success()) {
            faceRsp.getJSONObject("words_result").toMap().forEach((s, o) -> {
                String words = (String) ((Map) o).get("words");
                switch (s) {
                    case "姓名": {
                        response.setName(words);
                        break;
                    }
                    case "民族": {
                        response.setNationality(words);
                        break;
                    }
                    case "住址": {
                        response.setAddress(words);
                        break;
                    }
                    case "公民身份号码": {
                        response.setNum(words);
                        break;
                    }
                    case "出生": {
                        response.setBirth(words);
                        break;
                    }
                    case "性别": {
                        response.setSex(words);
                        break;
                    }
                }
            });
        }
        return response;
    }

    @PostMapping("/personInfo")
    @ResponseBody
    public Object uploadPost() throws IOException {
        return null;
    }
}
